library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readr)
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
owid_covid_data <- read_csv("owid-covid-data.csv",
col_types = cols(date = col_date(format = "%Y-%m-%d")))
# Filtramos los datos
owid_covid_data_filtered <- owid_covid_data %>%
filter(location %in% c("United States", "Italy", "United Kingdom","Canada","Japan","Spain", "Germany", "France"))
# Agrupar los datos por intervalos de 7 días y calcular el promedio semanal de nuevas muertes confirmadas por millón de personas para cada país
owid_covid_data_filtered_weekly <- owid_covid_data_filtered %>%
mutate(week = cut(date, breaks = "1 week")) %>%
group_by(location, week) %>%
summarise(average_new_deaths_per_million = mean(new_deaths_per_million, na.rm = TRUE)) %>%
ungroup() %>%
mutate(week = as.Date(week))
## `summarise()` has grouped output by 'location'. You can override using the
## `.groups` argument.
# Visualizar los datos
grafico <- ggplot(data = owid_covid_data_filtered_weekly, aes(x = week, y = average_new_deaths_per_million, color = location)) +
geom_line(size = ifelse(owid_covid_data_filtered_weekly$location == "Spain", 1, 0.5)) +
geom_hline(yintercept = 0, color = "azure4", show.legend = FALSE) +
scale_color_manual(values=c("Spain"="red", "Canada"="brown4", "United States"="purple", "Italy"="cadetblue2", "France"="yellow", "Germany"="chartreuse3", "Japan"="grey", "United Kingdom"="pink"))+
labs(x=NULL, y=NULL, title = "Daily new confirmed COVID-19 deaths per million people",
subtitle = "7-day rolling average. Due to varying protocols and challenges in the attribution of the cause of death, the number of confirmed deaths may not accurately represent the true number of deaths caused by COVID-19.",
color = "Location") +
scale_y_continuous(limits = c(0, NA), breaks = seq(0, 18, by = 2)) +
scale_x_date(breaks = as.Date(c("2020-02-13", "2021-02-13", "2022-02-13", "2023-02-13", "2024-02-28")), date_labels = "%b %d, %Y", limits = c(as.Date("2020-02-12"), as.Date("2024-02-28"))) +
theme_minimal() +
theme(panel.background = element_blank(),
panel.grid.major.y = element_line(color = "grey", linetype = "dashed"),
panel.grid.minor = element_blank(),
plot.title = element_text(face = "bold", color = "azure4"),
plot.subtitle = element_text(color = "azure4"))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
grafico
## Warning: Removed 167 rows containing missing values (`geom_line()`).

grafico_interactivo<-ggplotly(grafico)
grafico_interactivo